import * as types  from "./mutation-types"
import {playMode} from "../common/js/config";
import {shuffle} from "../common/js/util";
import {saveSearch,removeSearch,removeAllSearch,saveplay,Savefavorite,Deletefavorite} from "../common/js/cache"
//
function FindexIndex(list,song){
      return   list.findIndex((item)=>{
                return item.id === song.id
        })
}

// 点击播放的异步函数
export const  selectPlay = function ({commit,state},{list,index}) {
        let newList = list.filter((song) => {
                return song.url != ""
        }) 
        commit(types.SET_SEQUENCE_LIST,newList)//顺序播放列表

        let newIndex = newList.findIndex((item) => {return item.id == list[index].id})
        if(state.mode === playMode.random){//判断当前的播放模式
         let randomList = shuffle(newList);
         commit(types.SET_PLAYLIST,randomList )//播放列表,随机播放的列表
         newIndex =  FindexIndex(randomList,newList[newIndex])
        }else{
          commit(types.SET_PLAYLIST,newList )//播放列表，默认的顺序播放
        }
        commit(types.SET_CURRENT_INDEX,newIndex)//当前播放的歌曲
        commit(types.SET_FULL_STATE,true),//全屏播放
        commit(types.SET_PLAYING_SATATE,true)//播放当前音乐
}
// 点击music-list播放的随机播放异步函数
export const randomPlay = function({commit,state},{list}){
        let newList = list.filter((song) => {
                return song.url != ""
        })
        commit(types.SET_MODE_STATE,playMode.random);
        commit(types.SET_SEQUENCE_LIST,newList)
        let randomList = shuffle(newList);
        commit(types.SET_PLAYLIST,randomList )//播放列表，改成随机播放
        commit(types.SET_CURRENT_INDEX,0)//当前播放的歌曲
        commit(types.SET_FULL_STATE,true),//全屏播放
        commit(types.SET_PLAYING_SATATE,true)//播放当前音乐


}



// 点击搜索结果中的歌曲插入到播放列表
export const insertSong = function ({commit,state},song){
        let playlist = state.playlist.slice()
        let sequenceList = state.sequenceList.slice()
        let currentIndex = state.currentIndex
        // 记录当前的歌曲
        let currentSong = playlist[currentIndex]
        // 查找当前的播放列表中是否有这个待插入的歌曲并返回索引
        let fpIndex = FindexIndex(playlist,song)

        // 因为是插入歌曲所以索引要+1
        currentIndex++ //将这个歌曲插入到当前播放的这首歌的后面
        playlist.splice(currentIndex,0,song)
        if(fpIndex > -1){//如果已经包含了这首歌,就删掉之前的
          //如果当前插入的序号大于列表中的序号
          if(currentIndex > fpIndex ){
                playlist.splice(fpIndex,1)
                // 整个数组的长度会减1
                currentIndex--
          }else{
                  playlist.splice(fpIndex+1,1)
          }
        }
        // 在sequencelist中插入的逻辑
        // 记录点击的这首歌想要插入的位置
        let currentSIndex = FindexIndex(sequenceList,currentSong) + 1

        // 在顺序播放列表查询是否有当前点击的这首歌
        let fsIndex = FindexIndex(sequenceList,song)

        // 在顺序列表中插入点击的这首歌
        sequenceList.splice(currentSIndex,0,song)

        if(fsIndex > -1){
                if(currentSIndex > fsIndex){
                        sequenceList.splice(fsIndex,1)
                }else{
                        sequenceList.splice(fsIndex+1,1)
                }
        }

        commit(types.SET_PLAYLIST,playlist)
        commit(types.SET_SEQUENCE_LIST,sequenceList)
        commit(types.SET_CURRENT_INDEX,currentIndex)
        commit(types.SET_FULL_STATE,true),//全屏播放
        commit(types.SET_PLAYING_SATATE,true)//播放当前音乐

}

// 保存搜索的结果
export const saveSearchHistory = function ({commit},query){
        commit(types.SET_SEARCH_HISTORY_STATE,saveSearch(query))
}
// 删除历史记录中某一项
export const removeSearchHistory = function ({commit},query){
        commit(types.SET_SEARCH_HISTORY_STATE,removeSearch(query))
}
// 删除所有的历史记录
export const removeAllHistory = function ({commit}){
        commit(types.SET_SEARCH_HISTORY_STATE,removeAllSearch())
}
// 删除播放列表中的某一项歌曲
export const deleteListOne = function ({commit,state},song){
        let playlist = state.playlist.slice()
        let sequenceList = state.sequenceList.slice()
        let currentIndex = state.currentIndex
        let pIndex = FindexIndex(playlist,song)
        playlist.splice(pIndex,1)
        let sIndex = FindexIndex(sequenceList,song)
        sequenceList.splice(sIndex,1)
        if(currentIndex > pIndex || currentIndex === playlist.length){
             currentIndex--
        }
        commit(types.SET_PLAYLIST,playlist)
        commit(types.SET_SEQUENCE_LIST,sequenceList)
        commit(types.SET_CURRENT_INDEX,currentIndex)

        if(!playlist.length){
             commit(types.SET_PLAYING_SATATE,false)
        }else{
             commit(types.SET_PLAYING_SATATE,true)
        }

}
//删除整个播放列表
export const deleteAllPlaylist = function ({commit,state}){
        commit(types.SET_PLAYLIST,[])
        commit(types.SET_SEQUENCE_LIST,[])
        commit(types.SET_PLAYING_SATATE,false)
        commit(types.SET_CURRENT_INDEX,-1)

}

// 保存播放记录
export const savePlayHistory = function ({commit,state},song){
        commit(types.SET_PLAY_HISTORY_STATE,saveplay(song))
}

// 点击添加，保存到喜欢的记录
export const saveFavorite = function ({commit,state},song){
        commit(types.SET_FAVORITE_LIST_STATE,Savefavorite(song))
}

// 点击删除喜欢的记录，
export const deleteFavorite = function ({commit,state},song){
        commit(types.SET_FAVORITE_LIST_STATE,Deletefavorite(song))
}
